from pathlib import Path

from gemini_antenna.db import AntennaDB
from gemini_antenna.multiFeedParsing import parsegemsub, parsexml, parsetwtxt


def test_gemsub(db: AntennaDB):
    agelimit = 1605000000
    with open(Path("tests") / "feed.gmi") as file:
        entries = parsegemsub(file.read(), "gemini://gemini.jrandom.net/gemlog/")
    assert db.insertFeedEntries(entries) == 3
    assert db.insertFeedEntries(entries, agelimit) == 2

    assert len(db.getEntries()) == 3
    db.pruneDB(agelimit)
    assert len(db.getEntries()) == 2

    db.deleteFeeds("gemini://gemini.jrandom.net/~user/gemlog/")
    assert len(db.getEntries()) == 2
    db.deleteFeeds("gemini://gemini.jrandom.net/gemlog/")
    assert len(db.getEntries()) == 0


def test_atom(db: AntennaDB):
    agelimit = 1605000000
    with open(Path("tests") / "feed.atom") as file:
        entries = parsexml(file.read(), "gemini://gemini.jrandom.net/gemlog/")
    assert db.insertFeedEntries(entries) == 3
    assert db.insertFeedEntries(entries, agelimit) == 2

    assert len(db.getEntries()) == 3
    db.pruneDB(agelimit)
    assert len(db.getEntries()) == 2

    db.deleteFeeds("gemini://gemini.jrandom.net/~user/gemlog/")
    assert len(db.getEntries()) == 2
    db.deleteFeeds("gemini://gemini.jrandom.net/gemlog/")
    assert len(db.getEntries()) == 0


def test_twtxt(db: AntennaDB):
    agelimit = 1450000000
    with open(Path("tests") / "twtxt.txt") as file:
        entries = parsetwtxt(file.read(), "gemini://example.com/twtxt.txt")
    assert db.insertTwtxtEntries(entries) == 4

    assert len(db.getTwts()) == 4
    db.pruneDB(agelimit)
    assert len(db.getTwts()) == 3

    db.deleteFeeds("gemini://example.com/~user/twtxt.txt")
    assert len(db.getTwts()) == 3
    db.deleteFeeds("gemini://example.com/twtxt.txt")
    assert len(db.getTwts()) == 0
